﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Net;
using GPSProvider.Location;

namespace ECheck
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// host是服务器地址
        /// 获取巡检信息的地址是host?command=downloadtask&taskname=巡检任务
        /// 上传巡检信息的地址是host?command=commit&content=xml信息;
        /// 
        /// 从数据库中取出来的线路数据statis均为=
        /// 设备的specs表示规格
        /// 设备的amount表示数量
        /// 任务名称和线路名称相同
        /// 本次项目用不到任务，作为日后扩展的接口
        /// 
        /*
         * status + - * = 分别代表增删改无变化
<数据列表>
  <线路 overlineid="1121" linename="桐塘618线" status="=">
    <杆塔 overlineid="1121" wireid="37371" starttower="" finishtower="83460" towername="22#" longitude="120.05606539872810" latitude="28.90496416486477" elevation="" installtype="架空裸导线" nodedist="45" status="=">
      <杆塔 overlineid="1121" wireid="37387" starttower="83460" finishtower="83461" towername="22#直线杆（塔）" longitude="120.05606539872810" latitude="28.90496416486477" elevation="" installtype="架空绝缘线" nodedist="62" status="=">
        <杆塔 overlineid="1121" wireid="37388" starttower="83461" finishtower="83450" towername="22(直线分支杆)" longitude="120.05606539872810" latitude="28.90496416486477" elevation="" installtype="架空裸导线" nodedist="95" status="=">
          <杆塔 overlineid="1121" wireid="37377" starttower="83450" finishtower="83462" towername="22（直线杆）" longitude="120.05606539872810" latitude="28.90496416486477" elevation="" installtype="架空裸导线" nodedist="59" status="*">
            <杆塔 overlineid="1121" wireid="37364" starttower="83462" finishtower="79073" towername="22#直线杆" longitude="120.05606539872810" latitude="28.90496416486477" elevation="" installtype="架空裸导线" nodedist="85" status="-">
                <设备 deviceid="111" devicename="某设备" devicetype="moutype" devicenum="10" status="+"/>
         *      <杆塔 overlineid="1121" wireid="42783" starttower="79073" finishtower="79082" towername="1#直线杆" longitude="120.05606539872810" latitude="28.90496416486477" elevation="" installtype="" nodedist="" status="="/>
            </杆塔>
            <杆塔 overlineid="1121" wireid="37389" starttower="83462" finishtower="83452" towername="22#直线杆" longitude="120.05606539872810" latitude="28.90496416486477" elevation="" installtype="架空裸导线" nodedist="69" status="=">
              <杆塔 overlineid="1121" wireid="37379" starttower="83452" finishtower="83446" towername="22#直线杆" longitude="120.05606539872810" latitude="28.90496416486477" elevation="" installtype="架空裸导线" nodedist="96" status="=">
                <杆塔 overlineid="1121" wireid="37373" starttower="83446" finishtower="83437" towername="22#直线杆" longitude="120.05606539872810" latitude="28.90496416486477" elevation="" installtype="架空裸导线" nodedist="64" status="="/>
              </杆塔>
            </杆塔>
          </杆塔>
        </杆塔>
      </杆塔>
    </杆塔>
  </线路>
</数据列表>
        */
        const string overlineidtag = "overlineid";
        const string wireidtag = "wireid";
        const string toweridtag = "towerid";
        const string deviceidtag = "deviceid";
        const string starttowertag = "starttower";
        const string finishtowertag = "finishtower";
        const string belonglinetag = "belongline";
        const string belongtowertag = "belongtower";
        const string longitudetag = "longitude";
        const string latitudetag = "latitude";
        const string elevationtag = "elevation";
        const string editabletag = "editable";
        const string devicenametag = "devicename";
        const string daoxiantag = "导线";
        const string gantatag = "杆塔";
        const string shuxingtag = "属性";
        const string xianlutag = "线路";
        const string shebeitag = "设备";
        const string columnnametag = "columnname";
        const string valuetag = "value";
        const string aliasnametag = "aliasname";
        const string statustag = "status";
        const string titletag = "title";
        const string shujuliebiaotag = "数据列表";
        const string xianluliebiaotag = "线路列表";
        const string gantaliebiaotag = "杆塔列表";
        const string isdigitaltag = "isdigital";

        /// </summary>
        const string dlfilename = "dl_9.xml";
        const string dicfilename = "dic.xml";
        const string hostfilename = "dlhost.txt";
        static string host = "192.168.1.100";
        XmlDocument dataxml;
        XmlDocument dicXml = new XmlDocument();
        public Form1()
        {
            InitializeComponent();
            setcurrentCommand(null);
            if (File.Exists(hostfilename))
            {
                StreamReader sr = File.OpenText(hostfilename);
                host = sr.ReadLine();
                sr.Close();
                this.serverTextbox.Text = host;
            }
            else
            {
                this.serverTextbox.Text = host;
            }
            dataxml = new XmlDocument();
            if (File.Exists(dlfilename))
            {
                dataxml.Load(dlfilename);
            }
            else
            {
                initDLFile();
            }
            panel1.Controls.Clear();
            treeView1.BeforeExpand += new TreeViewCancelEventHandler(treeView1_BeforeExpand);
            initTree();
        }
        /// <summary>
        /// 节点打开前事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Parent == null)
            {
                e.Node.Nodes.Clear();
                XmlNode node = e.Node.Tag as XmlNode;
                inittree(node, e.Node);
            }
        }
        private bool IsUsed(XmlNode wirenode,string finishtower)
        {            
            XmlNodeList xmlnodes = SelectNodes(wirenode.ParentNode, daoxiantag, finishtowertag, finishtower);
            return xmlnodes.Count != 0;
        }
        void inittree(XmlNode parentxmlnode, TreeNode parenttreenode)
        {
            parenttreenode.Nodes.Clear();
            if (parentxmlnode.Name == xianlutag)
            {
                XmlNodeList nodelist = SelectNodes(parentxmlnode as XmlElement,daoxiantag,starttowertag,"");
                foreach(XmlNode starttowernode in nodelist)
                {                    
                    TreeNode newnode = parenttreenode.Nodes.Add(getTitle(starttowernode));
                    newnode.Tag = starttowernode as XmlElement;
                    inittree(starttowernode, newnode);
                }
            }
            else if (parentxmlnode.Name == daoxiantag)
            {
                string finishtowervalue = getAtt(parentxmlnode as XmlElement, finishtowertag);
                XmlNodeList wirenodelist = SelectNodes(parentxmlnode.ParentNode as XmlElement, daoxiantag, starttowertag, finishtowervalue);
                foreach (XmlNode wirenode in wirenodelist)
                {
                    TreeNode newnode = parenttreenode.Nodes.Add(getTitle(wirenode));
                    newnode.Tag = wirenode;
                    inittree(wirenode, newnode);
                }
                XmlNodeList devicenodelist = parentxmlnode.SelectNodes(shebeitag + "[@" + statustag + "!='-']");
                foreach (XmlNode devicenode in devicenodelist)
                {
                    TreeNode newnode = parenttreenode.Nodes.Add(getTitle(devicenode));
                    newnode.Tag = devicenode;
                }
                parenttreenode.Expand();
            }
        }
        /// <summary>
        /// 初始化线路树
        /// </summary>
        public void initTree()
        {
            if (!File.Exists(dicfilename))
            {
                return;
            }
            dicXml.Load(dicfilename);
            XmlNode node = dicXml.DocumentElement.SelectSingleNode("埋设类型");
            if (node == null)
            {
            }
            else
            {
                string installtypes = node.InnerText;
                //installtypecomboBox1.Items.Clear();
                string[] installtypeArray = installtypes.Split(',');
                foreach (string str in installtypeArray)
                {
                    //installtypecomboBox1.Items.Add(str);
                }
            }
            XmlNodeList nodelist = dicXml.DocumentElement.SelectNodes(shebeitag);
            //devicenamecombobox.Items.Clear();
            foreach (XmlNode node1 in nodelist)
            {
               // devicenamecombobox.Items.Add(node1.Attributes["name"].Value);
            }
            treeView1.Nodes.Clear();
            XmlNodeList lineEleList = dataxml.SelectNodes(shujuliebiaotag + "/" + xianluliebiaotag + "/" + xianlutag + "[@" + statustag + "!='-']");
            foreach (XmlNode lineNode in lineEleList)
            {
                //增加线路节点                
                string linename = getTitle(lineNode);
                TreeNode lineTreeNode = treeView1.Nodes.Add(linename);
                lineTreeNode.Tag = lineNode;
                lineTreeNode.Nodes.Add("");
            }
            tabControl1.SelectedIndex = 0;
        }
        /// <summary>
        /// 获取线路、导线、设备的名称
        /// </summary>
        /// <param name="ele"></param>
        /// <returns></returns>
        private string getTitle(XmlNode ele)
        {
            XmlNode titleatt = ele.SelectSingleNode(shuxingtag + "[@" + columnnametag + "='" + ele.Attributes[titletag].Value + "']");
            string statusvalue = ele.Attributes[statustag].Value;
            statusvalue = statusvalue == "=" ? "" : statusvalue;
            return titleatt.Attributes[valuetag].Value + "  " + statusvalue;
        }
        /// <summary>
        /// 获取线路、导线、设备的属性att的值
        /// </summary>
        /// <param name="xmlele">线路、导线、设备的dom节点</param>
        /// <param name="att">属性名 columnname</param>
        /// <returns></returns>
        private string getAtt(XmlNode xmlele, string att)
        {
            return xmlele.SelectSingleNode(shuxingtag + "[@" + columnnametag + "='" + att + "']/@" + valuetag).Value;
        }
        private void setAtt(XmlNode xmlnode, string att, string value)
        {
            if (att == finishtowertag && value == "")
            {
                MessageBox.Show("终止杆塔ID不能为空！");
                return;
            }
            string status = xmlnode.Attributes["status"].Value;
            if (status != "+")
            {
                xmlnode.Attributes["status"].Value = "*";
            }
            xmlnode.SelectSingleNode(shuxingtag + "[@" + columnnametag + "='" + att + "']/@" + valuetag).Value = value;
        }
        /// <summary>
        /// 获取线路、导线、杆塔、设备的tagname为tagname的，columnname为att，值为value的dom节点
        /// </summary>
        /// <param name="xmlele"></param>
        /// <param name="att"></param>
        /// <param name=value></param>
        /// <returns></returns>
        private XmlNodeList SelectNodes(XmlNode xmlele, string tagname,string att, string value)
        {
            if (att == finishtowertag && value == "")
            {
                MessageBox.Show("终止杆塔ID不能为空！");
                return xmlele.SelectNodes("abcdef");
            }
            return xmlele.SelectNodes(tagname + "[@" + statustag + "!='-' and " + shuxingtag + "[@" + columnnametag + "='" + att + "'and @" + valuetag + "='" + value + "']]");
        }
        /// <summary>
        /// 下载任务
        /// 同时需要下载数据字典
        /// </summary>
        /// <param name="taskname"></param>
        public void loadtask(string taskname)
        {
            //下载数据字典
            if (!downloadDic())
            {
                return;
            }
            try
            {
                HttpWebRequest webRequest = WebRequest.Create("http://" + host + "/dlgis/downloadtomobile.aspx?random=" + Guid.NewGuid().ToString() + "&lineids=" + taskname) as HttpWebRequest;
                webRequest.Timeout = 30 * 60 * 1000;
                webRequest.ReadWriteTimeout = 30 * 60 * 1000;
                //string context = taskname;
                //byte[] contextBytes = Encoding.UTF8.GetBytes(context);
                //webRequest.Method = "POST";
                //webRequest.SendChunked = true;
                //webRequest.AllowWriteStreamBuffering = true;
                //webRequest.ContentType = "text/plain";
                //webRequest.ContentLength = contextBytes.Length;
                //using (Stream reqStream = webRequest.GetRequestStream())
                //{
                //    reqStream.Write(contextBytes, 0, contextBytes.Length);
                //    reqStream.Close();
                //}
                using (WebResponse webResponse = webRequest.GetResponse())
                {
                    string content = acceptWebResponse(webResponse);
                    if (content == "")
                    {
                        MessageBox.Show("没有下载到相关线路！");
                        return;
                    }
                    dataxml.InnerXml = content;
                    dataxml.Save(dlfilename);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("下载线路失败！\n原因：" + ex.Message);
            }
        }
        private string acceptWebResponse(WebResponse webResponse)
        {
            Stream stream = webResponse.GetResponseStream();

            int i = 0;
            List<byte> byteslist = new List<byte>();
            i = stream.ReadByte();
            while (i != -1)
            {
                byteslist.Add((byte)i);
                i = stream.ReadByte();
            }
            stream.Close();
            byte[] contentbytes = byteslist.ToArray();
            string content = Encoding.UTF8.GetString(contentbytes, 0, contentbytes.Length);
            return content;
        }
        /// <summary>
        /// 点击下载线路按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void downloadlines_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 1;
            if (!downloadDic())
            {
                return;
            }
            XmlNodeList transnodes = dicXml.SelectNodes("元数据/变电站");
            if (transnodes != null)
            {
                this.treeViewSelectLine.Nodes.Clear();
                foreach (XmlNode node in transnodes)
                {
                    TreeNode treenode = treeViewSelectLine.Nodes.Add(node.Attributes["name"].Value);
                    XmlNodeList linenodes = node.SelectNodes(xianlutag);
                    foreach (XmlNode node2 in linenodes)
                    {
                        treenode.Nodes.Add(node2.Attributes["name"].Value).Tag = node2.Attributes["overlineid"].Value;
                    }
                }
            }
            else
            {
                MessageBox.Show("没有下载到线路数据！");
                return;
            }
            panel1.Controls.Clear();
            panel1.Controls.Add(downloadlinepanel2);
            setcurrentCommand(downloadlinecommand);
        }
        XmlElement currentWire = null;
        XmlElement currentDevice = null;
        XmlElement currentLine = null;
        /// <summary>
        /// 选中线路数上节点后触发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            panel1.Controls.Clear();            
            currentDevice = null;
            currentWire = null;
            TreeNode node = e.Node;
            XmlElement xmlElem = node.Tag as XmlElement;
            //一级子节点
            if (xmlElem.Name == xianlutag)
            {
                setcurrentCommand(lineeditcommand);
                currentLine = xmlElem;
                initPanel(panel1, xmlElem,true);
            }
            else if (xmlElem.Name == daoxiantag)
            {
                setcurrentCommand(wireeditcommand);
                currentWire = xmlElem;
                initPanel(panel1, xmlElem,true);

            }
            else if (xmlElem.Name == shebeitag)
            {
                setcurrentCommand(deviceeditcommand);
                currentDevice = node.Tag as XmlElement;
                initPanel(panel1, currentDevice, true);
            }
        }
        private void initPanel(Panel panel, XmlElement xmlElem,bool clear)
        {
            if(clear)
                panel.Controls.Clear();
            XmlNodeList nodes = xmlElem.SelectNodes("属性");
            int x = 10;
            int y = 10;
            foreach (XmlNode attnode in nodes)
            {
                XmlElement xmlele = attnode as XmlElement;
                Label label = new Label();
                label.Width = 90;
                label.TextAlign = ContentAlignment.TopRight;
                label.Text = xmlele.GetAttribute(aliasnametag) + ":";
                label.Height = 15;
                label.Location = new Point(x, y);
                TextBox textbox = new TextBox();
                textbox.Width = 100;
                textbox.MaxLength = 50;
                textbox.Text = xmlele.GetAttribute(valuetag);
                textbox.Enabled = xmlele.GetAttribute(editabletag).ToLower() == "true";                
                textbox.Location = new Point(x + 95, y - 3);
                textbox.Tag = xmlele;
                y += 25;
                panel.Controls.Add(label);
                panel.Controls.Add(textbox);
            }
            if (xmlElem.Name == daoxiantag)
            {
                string finishtower = getAtt(xmlElem, finishtowertag);
                XmlNodeList cnodes = SelectNodes(dataxml as XmlNode,shujuliebiaotag + "/" + gantaliebiaotag + "/" + gantatag , toweridtag, finishtower);
                if (cnodes.Count != 0)
                {
                    XmlNode cnode = cnodes[0];
                    nodes = cnode.SelectNodes(shuxingtag);
                    Label templabel = new Label();
                    templabel.Width = 90;
                    templabel.Height = 15;
                    templabel.Text = "终点杆塔基本属性";
                    templabel.Location = new Point(x, y);
                    panel.Controls.Add(templabel);
                    y += 25;
                    foreach (XmlNode attnode in nodes)
                    {
                        XmlElement xmlele = attnode as XmlElement;
                        Label label = new Label();
                        label.Width = 90;
                        label.TextAlign = ContentAlignment.TopRight;
                        label.Text = xmlele.GetAttribute(aliasnametag) + ":";
                        label.Height = 15;
                        label.Location = new Point(x, y);
                        TextBox textbox = new TextBox();
                        textbox.MaxLength = 50;
                        textbox.Width = 100;
                        textbox.Text = xmlele.GetAttribute(valuetag);
                        textbox.Enabled = xmlele.GetAttribute(editabletag).ToLower() == "true";
                        textbox.Location = new Point(x + 95, y - 3);
                        textbox.Tag = xmlele;
                        y += 25;
                        panel.Controls.Add(label);
                        panel.Controls.Add(textbox);
                    }
                }
            }
        }
        Gps gps = null;
        /// <summary>
        /// 点击重新定位按钮，重新定位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public bool ReLocation(object sender, EventArgs e)
        {
            if (gps == null)
            {
                gps = new Gps();
            }
            if (!gps.Opened)
            {
                gps.Open();
            }
            if (currentWire != null)
            {
                GpsPosition pos = gps.GetPosition();
                if (pos.Longitude == 0 || pos.Latitude == 0)
                {
                    return false;
                }
                foreach (Control control in panel1.Controls)
                {
                    if (control is TextBox)
                    {
                        if (control.Tag != null)
                        {
                            if (control.Tag is XmlElement)
                            {
                                XmlElement xmlele = control.Tag as XmlElement;
                                if (xmlele.GetAttribute("columnname").ToLower() == "longitude")
                                {
                                    control.Text = pos.Longitude.ToString();
                                }
                                else if (xmlele.GetAttribute("columnname").ToLower() == "latitude")
                                {
                                    control.Text = pos.Latitude.ToString();
                                }
                                else if (xmlele.GetAttribute("columnname").ToLower() == "elevation")
                                {
                                    control.Text = pos.SeaLevelAltitude.ToString();
                                }
                            }
                        }
                    }
                }
                return true;
            }
            return false;
        }
        /// <summary>
        /// 设置设备类型和数量
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void verifyeditdevicebutton1_Click(object sender, EventArgs e)
        {
            savemodify();
        }
        /// <summary>
        /// 重新定位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void relocationbutton2_Click(object sender, EventArgs e)
        {
            if (!ReLocation(sender, e))
            {
                MessageBox.Show("GPS不能定位或者没有选中杆塔！");
            }
        }
        /// <summary>
        /// 获取GPS设置坐标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void verifywireedit_click(object sender, EventArgs e)
        {
            savemodify();
        }
        /// <summary>
        /// 下载数据字典按钮按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void downloaddic_Click(object sender, EventArgs e)
        {
            if (downloadDic())
            {
                MessageBox.Show("数据字典下载成功！");
            }
        }
        /// <summary>
        /// 下载数据字典
        /// </summary>
        private bool downloadDic()
        {
            try
            {
                HttpWebRequest webRequest = WebRequest.Create("http://" + host + "/dlgis/downloadmetadata.aspx?random=" + Guid.NewGuid().ToString()) as HttpWebRequest;
                webRequest.Timeout = 30 * 60 * 1000;
                webRequest.ReadWriteTimeout = 30 * 60 * 1000;
                string content = acceptWebResponse(webRequest.GetResponse());
                if (File.Exists(dicfilename))
                {
                    File.Delete(dicfilename);
                }
                dicXml.LoadXml(content);
                dicXml.Save(dicfilename);
                XmlNode node = dicXml.DocumentElement.SelectSingleNode("埋设类型");
                if (node == null)
                {

                }
                else
                {
                    //string installtypes = node.InnerText;
                    //installtypecomboBox1.Items.Clear();
                    //string[] installtypeArray = installtypes.Split(',');
                    //foreach (string str in installtypeArray)
                    //{
                    //    installtypecomboBox1.Items.Add(str);
                    //}
                }
                //XmlNodeList nodelist = dicXml.DocumentElement.SelectNodes(shebeitag);
                //devicenamecombobox.Items.Clear();
                //foreach (XmlNode node1 in nodelist)
                //{
                //    devicenamecombobox.Items.Add(node1.Attributes["name"].Value);
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show("下载数据字典失败！\n原因：" + ex.Message);
                return false;
            }
            return true;
        }
        /// <summary>
        /// 设置服务器地址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void confighost_Click(object sender, EventArgs e)
        {
            panel1.Controls.Clear();
            panel1.Controls.Add(configpanel2);
            setcurrentCommand(confighostcommand);
            tabControl1.SelectedIndex = 1;
            tabControl1.Refresh();
        }
        /// <summary>
        /// 写入服务器地址到hostfilename文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void verifyconfighost_Click(object sender, EventArgs e)
        {
            StreamWriter sw = File.CreateText(hostfilename);
            sw.WriteLine(serverTextbox.Text);
            sw.Flush();
            sw.Close();
            host = serverTextbox.Text;
        }
        /// <summary>
        /// 下载线路，初始化线路、杆塔、设备树
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void verifydownloadlines_Click_1(object sender, EventArgs e)
        {
            XmlNodeList nodes = dataxml.SelectNodes("//*[@status!='=']");
            if (nodes.Count != 0)
            {
                MessageBox.Show("手持设备上有未提交的修改，请提交修改后再下载新线路！");
                return;
            }
            string selectedlines = "";
            foreach (TreeNode node in treeViewSelectLine.Nodes)
            {
                foreach (TreeNode node2 in node.Nodes)
                {
                    if (node2.Checked)
                        selectedlines += node2.Tag.ToString() + " ";
                }
            }
            selectedlines = selectedlines.TrimEnd(' ');
            if (selectedlines.Length == 0)
            {
                MessageBox.Show("没有选中线路");
                return;
            }
            loadtask(selectedlines);
            initTree();
            MessageBox.Show("线路下载结束！");
        }
        /// <summary>
        /// 删除数据按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void deldatabutton_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 0;
            if (dataxml == null || !File.Exists(dlfilename) || dataxml.DocumentElement.InnerXml == "")
            {
                MessageBox.Show("当前没有数据！");
                return;
            }
            if (MessageBox.Show("请慎重操作，删除数据会导致没有提交的修改丢失！是否要继续？", "注意", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {               
                treeView1.Nodes.Clear();
                initDLFile();
                setcurrentCommand(null);
                MessageBox.Show("删除完成！");
                panel1.Controls.Clear();
            }
        }

        private void initDLFile()
        {
            dataxml.LoadXml("<数据列表><线路列表/><杆塔列表/></数据列表>");
            dataxml.Save(dlfilename);
        }

        /// <summary>
        /// 提交修改按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void commit_Click(object sender, EventArgs e)
        {

            tabControl1.SelectedIndex = 0;
            if (dataxml == null || !File.Exists(dlfilename) || dataxml.DocumentElement.InnerXml == "")
            {
                MessageBox.Show("手持机上没有线路数据！");
                return;
            }
            string error = "";
            string lines = "";
            int i = SubmitCheckWork(out error, out lines);
            if (i == 0)
            {
                MessageBox.Show("没有做过任何修改，无需提交！");
            }
            else if (i == -1)
            {
                MessageBox.Show("上传修改结果失败，\n原因:" + error);
            }
            else
            {
                if (lines != "")
                {
                    lines = lines.TrimEnd(' ');
                    loadtask(lines);
                    initTree();
                }
                MessageBox.Show("修改结果提交成功!");
            }
        }
        /// <summary>
        /// 提交修改
        /// </summary>
        /// <param name="error">提交过程出现的错误信息</param>
        /// <returns>修改记录数</returns>
        private int SubmitCheckWork(out string error,out string lineids)
        {
            error = "";
            lineids = "";
            try
            {
                XmlNodeList nodes = dataxml.SelectNodes("//*[@status!='=']");
                if (nodes.Count == 0)
                {
                    return nodes.Count;
                }
                XmlNodeList invalidTowers = dataxml.SelectNodes("//杆塔");
                foreach (XmlNode towernode in invalidTowers)
                {
                    string id = getAtt(towernode,toweridtag);
                    if (dataxml.SelectSingleNode("//线路[@status!='-']/导线[@status!='-']/属性[@columnname='finishtower' and @value='" + id + "']") == null)
                    {
                        towernode.ParentNode.RemoveChild(towernode);
                    }
                }
                dataxml.Save(dlfilename);
                HttpWebRequest webRequest = WebRequest.Create("http://" + host + "/dlgis/ParseXml.aspx") as HttpWebRequest;
                webRequest.Timeout = 30 * 60 * 1000;
                webRequest.ReadWriteTimeout = 30 * 60 * 1000;
                string context = dataxml.InnerXml;
                byte[] contextBytes = Encoding.UTF8.GetBytes(context);
                webRequest.Method = "POST";
                webRequest.SendChunked = true;
                webRequest.AllowWriteStreamBuffering = true;
                webRequest.ContentType = "text/xml";
                webRequest.ContentLength = contextBytes.Length;
                using (Stream reqStream = webRequest.GetRequestStream())
                {
                    reqStream.Write(contextBytes, 0, contextBytes.Length);
                    reqStream.Close();
                }
                using (WebResponse webResponse = webRequest.GetResponse())
                {
                    lineids = acceptWebResponse(webResponse);
                }
                //将上传的xml备份，防止出现意外。
                dataxml.Save(DateTime.Now.ToString("yyyyMMddHHmmss") + ".xml");
                return nodes.Count;
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return -1;
            }
        }
        /// <summary>
        /// 关闭程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void close_Click_1(object sender, EventArgs e)
        {
            dataxml.Save(dlfilename);
            this.Close();
        }
        /// <summary>
        /// 点击新增设备按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addnewdevice_Click(object sender, EventArgs e)
        {
            XmlElement parentWire = null;
            TreeNode parentNode = null;
            if (currentDevice != null)
            {
                parentWire = currentDevice.ParentNode as XmlElement;
                parentNode = treeView1.SelectedNode.Parent;
            }
            else if(currentWire != null)
            {
                parentWire = currentWire;
                parentNode = treeView1.SelectedNode;
            }
            string belongline = getAtt(parentWire, belonglinetag);
            string finishtower = getAtt(parentWire, finishtowertag);
            XmlNode shebeiele = createnewnode(shebeitag);
            if(shebeiele == null){
                return;
            }
            setAtt(shebeiele, belonglinetag, belongline);
            setAtt(shebeiele, belongtowertag, finishtower);
            parentWire.AppendChild(shebeiele);
            dataxml.Save(dlfilename);            
            TreeNode treenode = parentNode.Nodes.Add(getTitle(shebeiele));
            parentNode.Expand();
            treenode.Tag = shebeiele;
            treeView1.SelectedNode = treenode;
        }
        /// <summary>
        /// 设备名称改变后触发规格列表更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void devicecombobox_TextChanged(object sender, EventArgs e)
        {
            //speccombobox.Items.Clear();
            //foreach (XmlNode node1 in dicXml.SelectNodes("//设备[@name='" + devicenamecombobox.Text + "']/spec"))
            //{
            //    speccombobox.Items.Add(node1.Attributes[value].Value.ToString());
            //}
        }
        /// <summary>
        /// 删除线路
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void removelinebutton_Click(object sender, EventArgs e)
        {
            if (currentLine == null || treeView1.Nodes.Count == 0)
            {
                MessageBox.Show("当前没有选中任何线路！");
                return;
            }
            if (MessageBox.Show("删除线路将导致线路及其所属的导线和设备一并删除，且不可恢复，是否一定要删除？", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button2) == DialogResult.No)
            {
                return;
            }
            if (currentLine.GetAttribute(statustag) == "+")
            {
                currentLine.ParentNode.RemoveChild(currentLine);                
            }
            else
            {
                currentLine.SetAttribute(statustag, "-");
                XmlNodeList daoxiannodes = currentLine.SelectNodes(daoxiantag);
                foreach (XmlNode daoxiannode in daoxiannodes)
                {
                    if (daoxiannode.Attributes[statustag].Value != "+")
                    {
                        daoxiannode.Attributes[statustag].Value = "-";
                    }
                    else
                    {
                        daoxiannode.ParentNode.RemoveChild(daoxiannode);
                    }
                }
                XmlNodeList shebeinodes = currentLine.SelectNodes(shebeitag);
                foreach (XmlNode shebeinode in shebeinodes)
                {
                    if (shebeinode.Attributes[statustag].Value != "+")
                    {
                        shebeinode.Attributes[statustag].Value = "-";
                    }
                    else
                    {
                        shebeinode.ParentNode.RemoveChild(shebeinode);
                    }
                }
            }
            treeView1.SelectedNode.Remove();                
            if(treeView1.Nodes.Count == 0)
            {
                panel1.Controls.Clear();
                setcurrentCommand(null);
                initDLFile();
            }
            dataxml.Save(dlfilename);
            MessageBox.Show("删除线路已完成！");
        }
        private XmlElement currentTower = null;
        /// <summary>
        /// 复制当前杆塔
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void copytower_Click_1(object sender, EventArgs e)
        {
            if (currentWire != null)
            {
                string finishtower = getAtt(currentWire,finishtowertag);
                foreach (Control control in panel1.Controls)
                {
                    if (control is TextBox)
                    {
                        if (control.Tag != null && control.Tag is XmlElement)
                        {
                            XmlElement xmlele = control.Tag as XmlElement;
                            
                            if (xmlele.GetAttribute(valuetag).ToLower() != control.Text.ToLower())
                            {
                                MessageBox.Show("当前导线尚未保存，请保存后再复制杆塔！");
                                return;
                            }
                        }
                    }
                }
                XmlNodeList tnodelist = SelectNodes(dataxml as XmlNode, shujuliebiaotag + "/" + gantaliebiaotag + "/" + gantatag, toweridtag, finishtower);
                if (tnodelist.Count == 0)
                {
                    currentTower = null;
                    MessageBox.Show("复制杆塔失败！");                    
                }
                else
                {

                    currentTower = tnodelist[0] as XmlElement;
                }

            }
        }
        /// <summary>
        /// 粘贴杆塔
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pastetower_Click_1(object sender, EventArgs e)
        {
            if (currentWire == null)
                return;
            if (this.currentTower == null)
            {
                MessageBox.Show("您还没有复制杆塔！");
                return;
            }
            else
            {
                if(IsUsed(currentWire,getAtt(currentTower,toweridtag)))
                {
                    MessageBox.Show("在这条线路中，你要粘贴的杆塔已经被使用，粘贴杆塔失败！");
                    return;
                }
                DialogResult result = MessageBox.Show("粘贴杆塔操作将会被直接保存，且不能恢复，你确认要粘贴杆塔吗？","警告",MessageBoxButtons.YesNo,MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button2);
                if (result == DialogResult.Yes)
                {
                    string finishtower = getAtt(currentWire, finishtowertag);
                    XmlNodeList nextnodes = SelectNodes(currentWire.ParentNode, daoxiantag, starttowertag, finishtower);
                    setAtt(currentWire, finishtowertag, getAtt(currentTower, toweridtag));
                    foreach (XmlNode nextnode in nextnodes)
                    {
                        setAtt(nextnode, starttowertag, getAtt(currentTower, toweridtag));
                    }
                    XmlNodeList shebeinodes = currentWire.SelectNodes(shebeitag);
                    foreach (XmlNode shebeinode in shebeinodes)
                    {
                        setAtt(shebeinode, belongtowertag, getAtt(currentTower, toweridtag));
                    }
                    foreach (Control control in panel1.Controls)
                    {
                        if (control is TextBox)
                        {
                            if (control.Tag != null && control.Tag is XmlElement)
                            {
                                XmlElement xmlele = control.Tag as XmlElement;                                
                                if (xmlele.Name == "属性")
                                {
                                    if (xmlele.GetAttribute("tablename").ToLower() == "tab_tower")
                                    {
                                        control.Text = getAtt(currentTower, xmlele.GetAttribute(columnnametag));
                                        control.Tag = currentTower.SelectSingleNode(shuxingtag + "[@" + columnnametag + "='" + xmlele.GetAttribute(columnnametag) + "']") as XmlElement;
                                    }
                                    else if (xmlele.GetAttribute(columnnametag).ToLower() == "finishtower")
                                    {
                                        control.Text = getAtt(currentTower,toweridtag);
                                    }
                                }
                            }
                        }
                    }
                }
            }            
        }

        private void treeViewSelectLine_AfterCheck(object sender, TreeViewEventArgs e)
        {
            TreeNode node = e.Node;
            if (node.Parent == null)
            {
                foreach (TreeNode node2 in node.Nodes)
                {
                    node2.Checked = node.Checked;
                }
            }
        }
        /// <summary>
        /// 点击新增线路按钮，点击按钮后显示新增线路面板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addnewline_Click_1(object sender, EventArgs e)
        {
            XmlElement xianluele = createnewnode(xianlutag);
            if (xianluele != null)
            {
                TreeNode treenode = treeView1.Nodes.Add(getTitle(xianluele));
                dataxml.SelectSingleNode(shujuliebiaotag + "/" + xianluliebiaotag).AppendChild(xianluele);
                dataxml.Save(dlfilename);
                treenode.Tag = xianluele as System.Xml.XmlElement;
                treeView1.SelectedNode = treenode;
            }
        }
        /// <summary>
        /// 确认线路修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void verifylinebutton_Click(object sender, EventArgs e)
        {
            savemodify();
        }
        private void savemodify()
        {
            foreach (Control control in panel1.Controls)
            {
                if (control is TextBox)
                {
                    TextBox tb = control as TextBox;
                    if (tb.Tag is XmlElement)
                    {
                        XmlElement xmlelem = tb.Tag as XmlElement;
                        if (xmlelem.GetAttribute(isdigitaltag).ToLower() == "true")
                        {
                            if (xmlelem.GetAttribute(editabletag).ToLower() == "true")
                            {
                                tb.Text = tb.Text.Trim();
                                if (tb.Text != "")
                                {
                                    try
                                    {
                                        double d = double.Parse(tb.Text);
                                        tb.Text = d.ToString();
                                    }
                                    catch (Exception ex)
                                    {
                                        MessageBox.Show(xmlelem.GetAttribute(aliasnametag) + " 应该为数字，请检查后再保存！");
                                        return;
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (tb.Text.IndexOfAny("<>?:-+'\"\\/=%^$".ToCharArray()) != -1)
                            {
                                MessageBox.Show(xmlelem.GetAttribute(aliasnametag) + " 有特殊字符(" + "<>?:-+'\"\\/=%^$" + ")，请检查后再保存！");
                                return;
                            }
                        }
                    }
                }
            }
            foreach (Control control in panel1.Controls)
            {
                if (control is TextBox)
                {
                    TextBox tb = control as TextBox;
                    if (tb.Tag is XmlElement)
                    {
                        XmlElement xmlelem = tb.Tag as XmlElement;
                        xmlelem.SetAttribute(valuetag, tb.Text);
                    }
                }
            }
            if (treeView1.SelectedNode != null)
            {
                if (treeView1.SelectedNode.Tag != null)
                {
                    if (treeView1.SelectedNode.Tag is XmlElement)
                    {
                        XmlElement xmlele = treeView1.SelectedNode.Tag as XmlElement;
                        if (xmlele.GetAttribute(statustag) != "+")
                        {
                            xmlele.SetAttribute(statustag, "*");
                        }
                        if (xmlele.Name == daoxiantag)
                        {
                            string finishtower = getAtt(xmlele,finishtowertag);
                            XmlNodeList cnodes = SelectNodes(dataxml as XmlNode, shujuliebiaotag + "/" + gantaliebiaotag + "/" + gantatag, toweridtag, finishtower);
                            if (cnodes.Count != 0)
                            {
                                XmlElement cele = cnodes[0] as XmlElement;
                                if(cele.GetAttribute(statustag) != "+")
                                    cele.SetAttribute(statustag, "*");
                            }
                        }
                        treeView1.SelectedNode.Text = getTitle(xmlele);
                    }
                }
            }
            dataxml.Save(dlfilename);
        }
        /// <summary>
        /// 向后插入一个导线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void insertwireafter_Click(object sender, EventArgs e)
        {

            if (currentWire == null)
            {
                MessageBox.Show("没有选中任何导线，只能在选中的导线后面插入！");
                return;
            }
            else
            {
                XmlNode daoxiannode = insertWireBefore(currentWire, false);
                if (daoxiannode == null)
                    return;
                string belongline = getAtt(currentWire, belonglinetag);
                setAtt(daoxiannode, belonglinetag, belongline);
                XmlElement xmlele = daoxiannode as XmlElement;
                currentWire.ParentNode.AppendChild(xmlele);
                dataxml.Save(dlfilename);
                TreeNode treenode = treeView1.SelectedNode.Nodes.Add(getTitle(xmlele));
                treeView1.SelectedNode.Expand();                
                treenode.Tag = xmlele;
                treeView1.SelectedNode = treenode;
                
            }
        }        
       /// <summary>
       /// 只删除导线节点
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void menuItem6_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("删除导线节点将会导致该导线及其所属设备被删除，切无法恢复！是否要继续", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                string starttower = getAtt(currentWire, starttowertag);                
                string finishtower = getAtt(currentWire, finishtowertag);
                XmlNodeList nextnodes = SelectNodes(currentWire.ParentNode, daoxiantag, starttowertag, finishtower);
                if (starttower == "" && nextnodes.Count > 1)
                {
                    if(MessageBox.Show("该导线下有两个导线，删除后将会形成双头线路，是否要继续？","警告",MessageBoxButtons.YesNo,MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button2) == DialogResult.No){
                        return;
                    }
                }
                if (nextnodes.Count != 0)
                {
                    foreach (XmlNode nextnode in nextnodes)
                    {
                        setAtt(nextnode, starttowertag, starttower);
                    }
                }
                
                if (currentWire.GetAttribute(statustag) == "+")
                {
                    currentWire.ParentNode.RemoveChild(currentWire);                    
                }
                else
                {
                    setAtt(currentWire, starttowertag, Guid.NewGuid().ToString());
                    setAtt(currentWire, finishtowertag, Guid.NewGuid().ToString());
                    currentWire.SetAttribute("status", "-");
                    XmlNodeList shebeinodes = currentWire.SelectNodes(shuxingtag);
                    foreach (XmlNode shebeinode in shebeinodes)
                    {
                        XmlElement shebeiele = shebeinode as XmlElement;
                        string status = shebeiele.GetAttribute(statustag);
                        if (status == "+")
                        {
                            shebeiele.ParentNode.RemoveChild(shebeiele);
                        }
                        else
                        {
                            shebeiele.SetAttribute(statustag, "-");
                        }
                    }
                }
                TreeNodeCollection treenodes = treeView1.SelectedNode.Nodes;
                TreeNode[] treenodeArray = new TreeNode[treenodes.Count];
                treenodes.CopyTo(treenodeArray, 0);
                foreach (TreeNode treenode in treenodeArray)
                {

                    if (treenode.Tag != null && treenode.Tag is XmlElement)
                    {
                        XmlElement xmlele = treenode.Tag as XmlElement;
                        if (xmlele.Name == daoxiantag)
                        {
                            treenode.Remove();
                            treeView1.SelectedNode.Parent.Nodes.Add(treenode);
                            treenode.Expand();
                        }
                    }
                }
                treeView1.SelectedNode.Remove();
                dataxml.Save(dlfilename);
            }
        }
        /// <summary>
        /// 删除设备
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void deldevicebutton1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要删除设备吗","警告",MessageBoxButtons.YesNo,MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                if (currentDevice != null)
                {
                    if (currentDevice.GetAttribute(statustag) == "+")
                    {
                        currentDevice.ParentNode.RemoveChild(currentDevice);
                        treeView1.SelectedNode.Remove();
                    }
                    else
                    {
                        currentDevice.SetAttribute(statustag, "-");
                    }
                    dataxml.Save(dlfilename);
                }
            }
        }
        /// <summary>
        /// 点击操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void command_Click(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex != 1)
            {
                tabControl1.SelectedIndex = 1;
            }
            if (panel1.Controls.Count != 0 && panel1.Controls[0].ContextMenu != null)
            {
                panel1.Controls[0].ContextMenu.Show(this, new Point(0, 0));
            }
        }
        private void setcurrentCommand(MenuItem command)
        {
            while (this.mainMenu1.MenuItems.Count != 1)
            {
                this.mainMenu1.MenuItems.RemoveAt(1);
            }
            if (command != null)
                this.mainMenu1.MenuItems.Add(command);
        }
        /// <summary>
        /// 向前插入一个导线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void insertwirebefore_Click(object sender, EventArgs e)
        {
            if (currentWire == null)
            {
                MessageBox.Show("没有选中任何导线，只能在选中的导线后面插入！");
                return;
            }
            else
            {
                XmlNode daoxiannode = insertWireBefore(currentWire,true);
                if(daoxiannode == null){
                    return;
                }
                string belongline = getAtt(currentWire, belonglinetag);
                setAtt(daoxiannode, belonglinetag, belongline);
                XmlElement xmlele = daoxiannode as XmlElement;
                currentWire.ParentNode.AppendChild(xmlele);
                dataxml.Save(dlfilename);
                TreeNode treenode = treeView1.SelectedNode.Parent.Nodes.Add(getTitle(xmlele));
                treenode.Tag = xmlele as System.Xml.XmlElement;
                TreeNode node2 = treeView1.SelectedNode;
                node2.Remove();
                treenode.Nodes.Add(node2);
                treenode.Parent.Expand();
                treeView1.SelectedNode = treenode;
                treenode.ExpandAll();
            }
        }
        private XmlNode insertWireBefore(XmlNode refNode,bool isbefore){
            string startower = "";
            string finishtower = "";
            XmlNode daoxianele = createnewnode(daoxiantag);
            XmlNode gantaele = createnewtower();
            if (daoxianele == null || gantaele == null)
                return null;
            //设置插入导线的finishtower
            setAtt(daoxianele, finishtowertag, getAtt(gantaele, toweridtag));
            
            if (refNode != null)
            {
                //如果refNode ！= null 获取参考导线的starttower 和 finishtower
                startower = getAtt(refNode, starttowertag);
                finishtower = getAtt(refNode, finishtowertag);
            }
            else
            {
                //如果参考导线是空，那么应该是创建导线
                isbefore = false;
                startower = finishtower = "";
            }
                        
            if (isbefore)
            {            
                //如果是向前插入导线，应当设置插入导线的starttower为参考导线的starttower
                //参考导线的starttower则应该是插入导线的finishtower
                setAtt(daoxianele, starttowertag, startower);
                setAtt(refNode, starttowertag, getAtt(gantaele, toweridtag));
            }
            else
            {             
                //如果是向后插入导线，则只需要设置插入导线的starttower为参考导线的finishtower
                //向后插入的导线，导线后面没有其它导线
                setAtt(daoxianele, starttowertag, finishtower);
            }
            dataxml.SelectSingleNode(shujuliebiaotag + "/" + gantaliebiaotag).AppendChild(gantaele);
            dataxml.Save(dlfilename);
            return daoxianele;
        }
        /// <summary>
        /// 创建线路、导线、设备
        /// </summary>
        /// <param name="tagName"></param>
        /// <returns></returns>
        private XmlElement createnewnode(string tagName)
        {
            XmlNode node = dicXml.SelectSingleNode("//模板/" + tagName + "");
            string idtag = "";
            if (tagName == xianlutag)
                idtag = overlineidtag;
            else if (tagName == daoxiantag)
                idtag = wireidtag;
            else if (tagName == shebeitag)
                idtag = deviceidtag;
            if (node == null)
            {
                MessageBox.Show("没有找到" + tagName + "模板，无法创建" + tagName + "，请检查后重新下载数据字典！");
                return null;
            }
            else
            {
                node = dataxml.ImportNode(node, true);
                setAtt(node, idtag, Guid.NewGuid().ToString());
                node.Attributes[statustag].Value = "+";
                XmlNode node1 = node.SelectSingleNode(shuxingtag + "[@" + columnnametag + "='" + node.Attributes[titletag].Value + "']");
                if (node1 == null)
                {
                    MessageBox.Show(tagName + "模板中没有指定title对应的属性，请检查后重新下载数据字典！");
                    return null;
                }
                else
                {
                    setAtt(node,node.Attributes[titletag].Value,"新增" + tagName);
                    return node as XmlElement;
                }
            }
        }
        private XmlElement createnewtower()
        {
            string tagName = gantatag;
            XmlNode node = dicXml.SelectSingleNode("//模板/" + tagName + "");
            if (node == null)
            {
                MessageBox.Show("没有找到" + tagName + "模板，无法创建" + tagName + "，请检查后重新下载数据字典！");
                return null;
            }
            else
            {                
                node = dataxml.ImportNode(node, true);
                setAtt(node, toweridtag, Guid.NewGuid().ToString());
                node.Attributes[statustag].Value = "+";
                return node as XmlElement;
            }
        }
        /// <summary>
        /// 创建导线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void createwire_Click(object sender, EventArgs e)
        {
            if (currentLine == null)
                return;
            if (currentLine.SelectNodes(daoxiantag + "[@status!='-']").Count != 0)
            {
                if (MessageBox.Show("该线路已经有导线，再创建导线将会出现多头线路，是否要继续？", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button2) == DialogResult.No)
                {
                    return;
                }
            }
            XmlNode daoxiannode = insertWireBefore(null, false);
            string belongline = getAtt(currentLine, overlineidtag);
            setAtt(daoxiannode, belonglinetag, belongline);
            if (daoxiannode == null)
                return;
            XmlElement daoxianele = daoxiannode as XmlElement;
            currentLine.AppendChild(daoxianele);
            dataxml.Save(dlfilename);
            TreeNode treenode = treeView1.SelectedNode.Nodes.Add(getTitle(daoxianele));
            treenode.Tag = daoxianele;
            treeView1.SelectedNode.Expand();
            treenode = treeView1.SelectedNode.Nodes[0];
            treeView1.SelectedNode = treenode;
        }

    }
}